package cn.xfdtool.slowsql.config;

import cn.xfdtool.slowsql.SQLSessionBeanPostProcessor;
import cn.xfdtool.slowsql.listener.SlowSQLMatchListener;
import cn.xfdtool.slowsql.listener.SlowSQLNotifyListener;
import cn.xfdtool.slowsql.listener.SlowSQLParserListener;
import cn.xfdtool.slowsql.listener.impl.LogSlowSQLNotifyListener;
import cn.xfdtool.slowsql.listener.impl.SimpleSlowSQLParserListener;
import cn.xfdtool.slowsql.listener.impl.SlowOrErrorSQLMatchListener;
import cn.xfdtool.slowsql.processor.SlowSQLProcessor;
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.Serializable;

/**
 * 慢SQL配置
 *
 * @author xfd
 * 2022/5/14
 */
@Data
@Configuration
@ConditionalOnProperty(prefix = "xfdtool.slowsql", name = "enable", havingValue = SlowSQLConfig.DEFAULT_SLOWSQL_ENABLE + "")
public class SlowSQLConfig implements Serializable {

    private static final long serialVersionUID = -3987071417608518260L;

    public static final String DEFAULT_SLOWSQL_ENABLE_NAME = "xfdtool.slowsql.enable";
    public static final boolean DEFAULT_SLOWSQL_ENABLE = true;

    public static final String DEFAULT_SLOWSQL_DEBUG_NAME = "xfdtool.slowsql.debug";
    public static final boolean DEFAULT_SLOWSQL_DEBUG = false;

    public static final String DEFAULT_SLOWSQL_PARSETIME_NAME = "xfdtool.slowsql.parsetime";
    public static final int DEFAULT_SLOWSQL_PARSETIME = 500;

    public static final String DEFAULT_SLOWSQL_TIME_NAME = "xfdtool.slowsql.time";
    public static final int DEFAULT_SLOWSQL_TIME = 3;

    public static final String DEFAULT_SLOWSQL_MAXROWCOUNT_NAME = "xfdtool.slowsql.maxrowcount";
    public static final int DEFAULT_SLOWSQL_MAXROWCOUNT = 20000;

    /** 是否启用 */
    @Value("${" + DEFAULT_SLOWSQL_ENABLE_NAME + ":" + DEFAULT_SLOWSQL_ENABLE + "}")
    private volatile boolean enable;

    /** 是否debug */
    @Value("${" + DEFAULT_SLOWSQL_DEBUG_NAME + ":" + DEFAULT_SLOWSQL_DEBUG + "}")
    private volatile boolean debug;

    /** 慢SQL解析时间频率, 单位: 毫秒(ms) */
    @Value("${" + DEFAULT_SLOWSQL_PARSETIME_NAME + ":" + DEFAULT_SLOWSQL_PARSETIME + "}")
    private volatile int slowSQLParseTime;

    /** 慢SQL时间值, 单位: 秒(s) */
    @Value("${" + DEFAULT_SLOWSQL_TIME_NAME + ":" + DEFAULT_SLOWSQL_TIME + "}")
    private volatile int slowSQLTime;

    /** 大SQL判断标准, 结果条数 */
    @Value("${" + DEFAULT_SLOWSQL_MAXROWCOUNT_NAME + ":" + DEFAULT_SLOWSQL_MAXROWCOUNT + "}")
    private volatile int maxRowCount;

    /**
     * 初始化SqlSessionFactory 扫描器
     * @return
     */
    @Bean
    public SQLSessionBeanPostProcessor createSQLSessionBeanPostProcessor() {
        return new SQLSessionBeanPostProcessor();
    }

    /**
     * 初始化慢SQL处理器
     * @return
     */
    @Bean
    public SlowSQLProcessor createSlowSQLProcessor() {
        return new SlowSQLProcessor();
    }

    /**
     * 构建慢SQL匹配器
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public SlowSQLMatchListener createSlowSQLMatchListener() {
        SlowOrErrorSQLMatchListener slowOrErrorSQLMatchListener = new SlowOrErrorSQLMatchListener(this);
        return slowOrErrorSQLMatchListener;
    }

    /**
     * sql数据解析
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public SlowSQLParserListener createSimpleSlowSQLParserListener() {
        return new SimpleSlowSQLParserListener();
    }

    /**
     * 消息通知
     * @return
     */
    @Bean
    @ConditionalOnMissingBean
    public SlowSQLNotifyListener createLogSlowSQLNotifyListener() {
        return new LogSlowSQLNotifyListener();
    }
}
